﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Meeting.Public;
using Newtonsoft.Json;

namespace Meeting.Web.Admin.BaseSet
{
    /// <summary>
    /// EmployeeOperation 的摘要说明
    /// </summary>
    public class EmployeeOperation : IHttpHandler
    {

        string MethodName = "";
        int nPageIndex = 1;
        int nPageSize = 4;

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json;charset=utf-8";
            MethodName = context.Request.Params["method"];
            nPageIndex = CommonFunction.varToIntDef(context.Request.Params["page"], 1);
            nPageSize = CommonFunction.varToIntDef(context.Request.Params["rows"], 15);
            //   defaultMedia = context.Request.Params["defaultMedia"];
            switch (MethodName.ToLower())
            {
                case "getemployee":
                    context.Response.Write(GetEmployee(context));
                    break;
                case "add":
                    context.Response.Write(SavaData(context));
                    break;
                case "edit":
                    context.Response.Write(SavaData(context));
                    break;
                case "delete":
                    context.Response.Write(DeleteData(context));
                    break;
                case "getrole":
                    context.Response.Write(GetRole(context));
                    break;
            }
        }

        //获取 role
        private string GetRole(HttpContext context)
        {
            BLL.Employee bll = new BLL.Employee();
            return bll.GetRole();
        }

        /// <summary>
        ///  
        /// </summary>
        /// <returns></returns>
        private string GetEmployee(HttpContext context)
        {
            string keyword = context.Request.Params["keyWord"];

            string sF = " 1=1 ";
            if (!string.IsNullOrEmpty(keyword)) sF += " And Name like '%" + keyword + "%' ";
            BLL.Employee bll = new BLL.Employee();
            return bll.GetPage(nPageIndex, nPageSize, sF);
             
        }

        //保存数据
        public string SavaData(HttpContext context)
        {
            Model.Employee model = new Model.Employee();
            model.RoleId = CommonFunction.VarToStr(context.Request["RoleId"]);
            model.RoleName = CommonFunction.VarToStr(context.Request["RoleName"]);
            model.Account = CommonFunction.VarToStr(context.Request["Account"]);
            model.Name = CommonFunction.VarToStr(context.Request["Name"]);
            model.Title = context.Request["Title"];
            model.Mobile = context.Request["Mobile"];
            model.PassWord = "111111";//context.Request["duty"];
            model.Comment = context.Request["Comment"];
            //提交数据
            Model.JsonResponse rsp = new Model.JsonResponse();

            BLL.Employee bll = new BLL.Employee();
            if (MethodName.ToLower() == "add")
            {
                rsp.success = bll.Add(model);
            }
            else
            {
                rsp.success = bll.Update(model);
            }
            rsp.Message = bll.errorString;
            return JsonConvert.SerializeObject(rsp);
        }
        //删除数据
        public string DeleteData(HttpContext context)
        {
            string Account = CommonFunction.VarToStr(context.Request["Account"] );
            //提交数据
            Model.JsonResponse rsp = new Model.JsonResponse();
            BLL.Employee bll = new BLL.Employee();
            if (bll.Delete(Account))
            {
                rsp.success = true;
            }
            else
            {
                rsp.success = false;
                rsp.Message = bll.errorString;
            }
            return JsonConvert.SerializeObject(rsp);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}